//
//  MyAccountTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/16.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "MyAccountTableViewController.h"
#import "AccountHeaderView.h"
#import "BindingPhoneTableViewController.h"
#import "LogInPassWordTableViewController.h"
#import "AlterPayPasswordTableViewController.h"
#import "BindingOilCardTableViewController.h"
#import "BindingBankCardTableViewController.h"
#import "MyAccountCell.h"
#import "homeCollectionViewCell.h"

#define ITEM_WIDTH WIDTH/3
#define MAR_WIDTH 5

@interface MyAccountTableViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)NSArray *accountArray;

@property(nonatomic,strong)UICollectionView *myAccountView;

@end

static NSString *const reuseIdentifier = @"AccountreuseIdentifier";

static NSString * const likeReuseIdentifier =  @"likeCollectionView";

@implementation MyAccountTableViewController

- (NSArray *)accountArray {
    if (!_accountArray) {
    
        NSString *accountPath = [[NSBundle mainBundle] pathForResource:@"MyAccount" ofType:@"plist"];
        
        _accountArray = [NSArray arrayWithContentsOfFile:accountPath];
    }
    return _accountArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addCollectionView];
    
    self.title = @"我的亿乐账户";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;

}

- (void)addCollectionView
{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.minimumLineSpacing = 0;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *myAccountView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT - 64) collectionViewLayout:flowlayout];
    _myAccountView = myAccountView;
    _myAccountView.delegate = self;
    _myAccountView.dataSource = self;
    _myAccountView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_myAccountView];
    //五个按钮
    [_myAccountView registerNib:[UINib nibWithNibName:@"MyAccountCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    //猜你喜欢
    [_myAccountView registerClass:[homeCollectionViewCell class] forCellWithReuseIdentifier:likeReuseIdentifier];
    //注册 头像
    [_myAccountView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"accountHeaderView"];
    [_myAccountView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"likeView"];

}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.accountArray.count;
    }else{
        return 6;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        MyAccountCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.iconImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"wodeyilezhanghu_%zd",indexPath.item]];
        cell.iconLab.text = [NSString stringWithFormat:@"%@",self.accountArray[indexPath.item]];
        cell.layer.borderColor = [UIColor colorWithRed:192.0/255.0 green:192.0/255.0 blue:192.0/255.0 alpha:0.6].CGColor;
        cell.layer.borderWidth = 0.5;
        return cell;
    }else{
        
        homeCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:likeReuseIdentifier forIndexPath:indexPath];
        
        cell.productImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"custom%ld.jpg",indexPath.item]];
        cell.productImage.contentMode = UIViewContentModeScaleToFill;
        
        cell.productName.text = @"2016春夏装韩流风顶级美女高俊熙小姐";
        cell.price.text = @"专享价: 180元";
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    
    }
   
}

#pragma mark >>>>>>>>collectionView 代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        
        if (indexPath.item == 0) {
            BindingPhoneTableViewController *bindingPhone = [[BindingPhoneTableViewController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:bindingPhone];
        } else if (indexPath.item == 1) {
            
            BindingBankCardTableViewController *bindingBankCard = [[BindingBankCardTableViewController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:bindingBankCard];
            
        } else if (indexPath.item == 2) {
            
            BindingOilCardTableViewController *bindingOilCard = [[BindingOilCardTableViewController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:bindingOilCard];
            
        } else if (indexPath.item == 3) {
            
            LogInPassWordTableViewController *logInPassword = [[LogInPassWordTableViewController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:logInPassword];
            
            
        } else {
            
            AlterPayPasswordTableViewController *alterPay = [[AlterPayPasswordTableViewController alloc] init];
            [self.navigationController pushForm:self.navigationController pushToAndHidden:alterPay];
        }
    
    }else{
        //section == 1
        
        
        
    }
    
    
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(ITEM_WIDTH,ITEM_WIDTH);
    }else{
        return CGSizeMake(WIDTH/2-MAR_WIDTH/2-5, HEIGHT/2.8);
    }
   
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return UIEdgeInsetsMake(0,MAR_WIDTH,0,MAR_WIDTH);
    }else{
        return UIEdgeInsetsZero;
    }
}

//header组头大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(WIDTH, HEIGHT/5);
    }else{
        return CGSizeMake(WIDTH,HEIGHT/10);
    }
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionReusableView *reusableView;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //
        
        if (indexPath.section == 0) {
            //头部视图
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"accountHeaderView" forIndexPath:indexPath];
            
            AccountHeaderView *headerView = [[AccountHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5)];
            headerView.textLabel.text = self.userNameString;
            [reusableView addSubview:headerView];
        }else{
            
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"likeView" forIndexPath:indexPath];
            
            //细线
            UIView *ThreadView = [[UIView alloc] init];
            ThreadView.backgroundColor = [UIColor blackColor];
            ThreadView.alpha = 0.1;
            [reusableView addSubview:ThreadView];
            
            UILabel *likeLabel = [[UILabel alloc] init];
            likeLabel.font = [UIFont systemFontOfSize:14];
            likeLabel.textAlignment = NSTextAlignmentCenter;
            likeLabel.text = @"猜你喜欢";
            likeLabel.textColor = DEFAULT_COLOR;
            likeLabel.backgroundColor = [UIColor whiteColor];
            [reusableView addSubview:likeLabel];
            
            [ThreadView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(reusableView.mas_centerX);
                make.centerY.equalTo(reusableView.mas_centerY).offset(HEIGHT/10/6);
                make.height.mas_equalTo(1);
                make.width.mas_equalTo(WIDTH-10);
            }];
            
            
            [likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(reusableView.mas_centerX);
                make.centerY.equalTo(ThreadView.mas_centerY);
                make.width.mas_equalTo(WIDTH/4);
            }];
            
        }
        return reusableView;
    }
    return  nil;
}



@end
